/*
     Copyright (C) 2011 Benny Mackney

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "FpsRegulator.h"

FpsRegulator::FpsRegulator()
{
    tickTimeStart = SDL_GetTicks();
    fpsGoal=30;
    lastFps=0;
    smoothedFps=0;
    tickTimeGoal = 1000/fpsGoal;
    smoothRate=1;
}

FpsRegulator::FpsRegulator(int FPS){
    tickTimeStart = SDL_GetTicks();
    fpsGoal=FPS;
    lastFps=0;
    smoothedFps=0;
    tickTimeGoal = 1000/fpsGoal;
    smoothRate=1;
}

FpsRegulator::~FpsRegulator()
{
    //dtor
}

void FpsRegulator::tick_wait(){
    int tickTime = (SDL_GetTicks()-tickTimeStart);
    if( tickTime < tickTimeGoal)
        {
            //Sleep the remaining frame time
            SDL_Delay( tickTimeGoal-tickTime);
        }
    lastFps = (float) 1000/(SDL_GetTicks()-tickTimeStart);
    tickTimeStart = SDL_GetTicks();
}
void FpsRegulator::set_fpsGoal(int FPS){
    fpsGoal=FPS;
    tickTimeGoal = 1000/fpsGoal;
}

float FpsRegulator::get_fps(){
    return lastFps;
}

float FpsRegulator::get_sfps(){
    smoothedFps = (lastFps+smoothedFps*(smoothRate))/(smoothRate+1);
    return smoothedFps;
}
